// 教学计划安排系统（Java Swing 版）无命名冲突版
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

public class CourseSchedulerUI extends JFrame {
    private JTextField maxCoursesField;
    private JTextArea resultArea;

    // 所有课程
    private static final List<String> courses = Arrays.asList(
            "职业生涯规划", "劳动实践I", "习近平总书记关于科技创新的重要论述(III)A", "程序设计基础I", "基础外语AI", "体育I",
            "思想道德与法治", "军事理论", "军训", "大学计算机基础", "高等数学AI", "劳动实践II", "程序设计基础II",
            "离散数学", "基础外语AII", "体育II", "中国近现代史纲要", "形势与政策I", "大学生心理健康", "程序设计课程设计",
            "认识实习", "思想政治理论课实践", "数据库应用", "高等数学AII", "大学物理AI", "创业基础", "面向对象程序设计",
            "数据结构", "数字电路与数字逻辑设计", "基础外语AIII", "体育III", "毛泽东思想和中国特色社会主义理论体系概论",
            "面向对象课程设计", "大学物理实验I", "大学物理AII", "线性代数A", "算法分析与设计", "软件工程",
            "计算机组成原理", "计算机组成原理实验", "基础外语AIV", "体育IV", "形势与政策II", "习近平新时代中国特色社会主义思想概论",
            "国家安全教育", "数据结构与算法课程设计", "电工电子课程设计", "大学物理实验II", "概率论与数理统计A",
            "数学软件与数学实验A", "计算机专业创新思维训练方法", "数据库系统原理", "软件体系结构", "Python高级应用",
            "嵌入式系统原理与应用A", "编译原理", "计算机网络A", "操作系统", "马克思主义基本原理", "数据采集与处理课程设计", "社会实践"
    );

    // 先修关系（示例）
    private static final Map<String, List<String>> prerequisites = new HashMap<>() {{
        put("程序设计基础II", List.of("程序设计基础I"));
        put("数据结构", List.of("程序设计基础II"));
        put("数据库应用", List.of("程序设计基础II"));
        put("面向对象程序设计", List.of("程序设计基础II"));
        put("面向对象课程设计", List.of("面向对象程序设计"));
        put("数据库系统原理", List.of("数据库应用"));
        put("算法分析与设计", List.of("数据结构"));
        put("编译原理", List.of("数据结构"));
        put("计算机组成原理实验", List.of("计算机组成原理"));
        put("数据结构与算法课程设计", List.of("数据结构"));
        put("Python高级应用", List.of("程序设计基础II"));
        put("操作系统", List.of("数据结构"));
        put("数据采集与处理课程设计", List.of("数据库系统原理"));
    }};

    public CourseSchedulerUI() {
        setTitle("教学计划安排系统（Swing版）");
        setSize(800, 600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        JPanel topPanel = new JPanel();
        topPanel.add(new JLabel("每学期最多课程数量:"));
        maxCoursesField = new JTextField("5", 5);
        topPanel.add(maxCoursesField);
        JButton generateButton = new JButton("生成计划");
        topPanel.add(generateButton);
        add(topPanel, BorderLayout.NORTH);

        resultArea = new JTextArea();
        resultArea.setFont(new Font("Monospaced", Font.PLAIN, 14));
        resultArea.setLineWrap(true);
        resultArea.setWrapStyleWord(true);
        add(new JScrollPane(resultArea), BorderLayout.CENTER);

        generateButton.addActionListener(e -> generatePlan());
    }

    private void generatePlan() {
        resultArea.setText("");
        int maxPerTerm;
        try {
            maxPerTerm = Integer.parseInt(maxCoursesField.getText());
            if (maxPerTerm <= 0) throw new NumberFormatException();
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, "请输入一个正整数！", "输入错误", JOptionPane.ERROR_MESSAGE);
            return;
        }

        List<List<String>> terms = scheduleCourses(maxPerTerm);
        for (int i = 0; i < terms.size(); i++) {
            resultArea.append("第" + (i + 1) + "学期: " + String.join("，", terms.get(i)) + "\n\n");
        }
    }

    private List<List<String>> scheduleCourses(int maxCoursesPerTerm) {
        Map<String, Integer> inDegree = new HashMap<>();
        Map<String, List<String>> graph = new HashMap<>();

        for (String course : courses) {
            inDegree.put(course, 0);
            graph.put(course, new ArrayList<>());
        }

        for (String course : prerequisites.keySet()) {
            for (String pre : prerequisites.get(course)) {
                graph.get(pre).add(course);
                inDegree.put(course, inDegree.get(course) + 1);
            }
        }

        Queue<String> queue = new LinkedList<>();
        for (String course : courses) {
            if (inDegree.get(course) == 0) {
                queue.offer(course);
            }
        }

        List<List<String>> result = new ArrayList<>();
        while (!queue.isEmpty()) {
            List<String> term = new ArrayList<>();
            int limit = Math.min(maxCoursesPerTerm, queue.size());
            for (int i = 0; i < limit; i++) {
                String c = queue.poll();
                term.add(c);
                for (String next : graph.get(c)) {
                    inDegree.put(next, inDegree.get(next) - 1);
                    if (inDegree.get(next) == 0) {
                        queue.offer(next);
                    }
                }
            }
            result.add(term);
        }

        return result;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new CourseSchedulerUI().setVisible(true);
        });
    }
}
